Swap OpenAI Swarm ⚒️ OpenAI Agents framework (modern)#50
Conversation
WalkthroughReplaces the Swarm-based orchestration with an OpenAI Agents multi-agent triage workflow: adds classification, general, and technical agents with an input-validation guardrail and triage routing; removes the Swarm module and template; updates python-uv templates to use the new agents module and dependencies. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor User
participant Req as AgentRequest
participant Runner
participant Triage as Triage Agent
participant Guardrail as Validation Guardrail
participant Classifier as Classification Agent
participant General as General Assistant
participant Tech as Technical Agent
participant Resp as AgentResponse
User->>Req: Send question
Req->>Runner: Runner.run(triage_agent, question, context)
Runner->>Triage: Invoke triage agent with input
Triage->>Guardrail: query_validation_guardrail(input)
Guardrail->>Classifier: classify input
Classifier-->>Guardrail: QueryClassification (is_valid, category, reasoning)
alt invalid input
Guardrail-->>Triage: tripwire_triggered=true (guardrail output)
Triage-->>Runner: Guardrail failure output
else valid input
Guardrail-->>Triage: classification info
alt category == "technical"
Triage->>Tech: Delegate question
Tech-->>Triage: Response
else
Triage->>General: Delegate question
General-->>Triage: Response
end
Triage-->>Runner: Final response
end
Runner-->>Resp: Return output
Resp-->>User: Deliver response
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Suggested reviewers
Poem
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. 📜 Recent review detailsConfiguration used: CodeRabbit UI 💡 Knowledge Base configuration:
You can enable these sources in your CodeRabbit configuration. 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
python-uv/templates.yaml (1)
198-214: Use the official PyPI package instead of a Git dependency
The OpenAI Agents SDK is published on PyPI under the nameopenai-agents, and its top-level import path isagents. Please update your template to install from PyPI and drop the Git URL:• In
python-uv/templates.yaml(around lines 200–204):- --quiet - - openai>=1.82.0 - - git+https://github.com/openai/openai-agents-python.git + - openai>=1.82.0 + - openai-agents>=0.1.0 # use the PyPI package - pydantic• No changes needed in
common/py/openai_agents.py—it correctly does
from agents import Agent, Runner, ….
🧹 Nitpick comments (5)
common/py/openai_agents.py (4)
5-8: Restrict classification.category to a known set for deterministic routing.Constrain
categoryto a limited set so the triage logic can rely on it consistently.Apply this diff:
class QueryClassification(BaseModel): is_valid: bool - category: str + category: Literal["technical", "general", "unknown"] reasoning: strAdd the import near the other imports (outside this range):
from typing import Literal # add with imports
30-38: Fail closed on guardrail errors and provide a safe fallback.If classification fails or returns an unexpected shape, the guardrail currently raises and bubbles up. Prefer a safe fallback that triggers the tripwire instead of throwing.
Apply this diff:
async def query_validation_guardrail(ctx, agent, input_data): - result = await Runner.run(classification_agent, input_data, context=ctx) - classification = result.final_output_as(QueryClassification) - return GuardrailFunctionOutput( - output_info=classification, - tripwire_triggered=not classification.is_valid, - ) + try: + result = await Runner.run(classification_agent, input_data, context=ctx) + classification = result.final_output_as(QueryClassification) + return GuardrailFunctionOutput( + output_info=classification, + tripwire_triggered=not classification.is_valid, + ) + except Exception as err: + ctx.logger.warning("Query validation guardrail failed: %s", err) + fallback = QueryClassification(is_valid=False, category="unknown", reasoning="Guardrail error") + return GuardrailFunctionOutput( + output_info=fallback, + tripwire_triggered=True, + )
39-47: Make routing criteria explicit to reduce LLM ambiguity.Tighten the triage instructions to explicitly use the guardrail’s classification for routing. This reduces prompt ambiguity and improves determinism.
Apply this diff:
triage_agent = Agent( name="Triage Agent", - instructions="You determine which specialist agent to use based on the user's query. Route technical questions to the Technical Specialist and general questions to the General Assistant.", + instructions=( + "Use the input validation guardrail classification to decide routing. " + "If classification.is_valid is false, do not hand off and ask the user to clarify or rephrase. " + "If classification.category == 'technical', hand off to the Technical Specialist. " + "Otherwise, hand off to the General Assistant." + ), handoffs=[general_assistant_agent, technical_agent], input_guardrails=[ InputGuardrail(guardrail_function=query_validation_guardrail), ], )
71-75: Validate empty input early for better UX.Short-circuit on empty/whitespace-only requests and return a quick message.
Apply this diff:
- user_question = await request.data.text() + user_question = await request.data.text() + if not user_question or not user_question.strip(): + return response.text("Please provide a question to process.")python-uv/templates.yaml (1)
198-214: Pin the git dependency and ensure pydantic v2 for reproducibility.Unpinned git installs can drift; pin to a commit SHA. Also ensure Pydantic v2 to match your
BaseModelusage across templates.Apply this diff (replace <COMMIT_SHA> with a specific commit):
- command: uv args: - add - --quiet - openai>=1.82.0 - - git+https://github.com/openai/openai-agents-python.git - - pydantic + - git+https://github.com/openai/openai-agents-python.git@<COMMIT_SHA> + - pydantic>=2.7
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (3)
common/py/openai_agents.py(1 hunks)common/py/swarm.py(0 hunks)python-uv/templates.yaml(1 hunks)
💤 Files with no reviewable changes (1)
- common/py/swarm.py
🔇 Additional comments (2)
common/py/openai_agents.py (1)
10-28: Nice modular agent setup and typed classification.Good separation of concerns with a classification agent, specialists, and a triage. Using a Pydantic model for the classifier output is a solid choice.
python-uv/templates.yaml (1)
209-213: LGTM: Template wiring to the new module is correct.The template correctly sources agent.py from common/py/openai_agents.py and keeps init.py consistent with other templates.
Summary by CodeRabbit
New Features
Refactor
Chores